package Groep3;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;

import Groep1.ReceiverSocket;

public class TestP2P {
	
	
	private BufferedReader inFromUser;
	private BufferedReader inFromOther;
	private DataOutputStream outToOther;
	private ArrayList<String> buffer;
	private boolean isTransmitting;
	
	private int sequanceNumber;
	private int ackNumber;
	
	private Socket clientSocket;
	private Socket connectionSocket;
	private ServerSocket idleSocket;
	
	
	public static void main (String argv[]) throws Exception {
		try
		{
			new TestP2P();
		}
		catch( Exception e)
		{
			e.printStackTrace();
		}
	}
	
	
	
	
	
	
	
	public TestP2P() throws Exception {
		sequanceNumber = (int)(100*Math.random());
		buffer = new ArrayList<String>();
		idleSocket = new ServerSocket(6789);
		//clientSocket = new Socket("localhost", 6789);
		
		connect();
		checkIncoming();
	}
	
	public void connect() throws Exception {
		System.out.println("Typ 'connect' om verbinding te openen");
		inFromUser = new BufferedReader(new InputStreamReader(System.in));
		Scanner in = new Scanner(System.in);
		if(in.nextLine().equals("connect")) {
			clientSocket = new Socket("localhost", 6789);

			System.out.println("client socket "+ clientSocket.getPort()); 
			
		}
		else {
			System.out.println("Not connected...");
			connect();
		}
	}
	
	public void checkIncoming() throws Exception {
		while(true) {
			connectionSocket = idleSocket.accept();
			System.out.println("Controleren op inkomende verbinding");
			inFromOther = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
			outToOther = new DataOutputStream(connectionSocket.getOutputStream());
			if(connectionSocket.isConnected()) {
				
//				run();
//				receive();
				System.out.println("Verbonden - gaat test() aanroepen");
				test();
				System.out.println("test() aangeroepen");
				break;

			}
		}
	}
	
	public void test() throws Exception {
		System.out.println("test() begint");
		boolean waiting = false;
		//while(true) {
			System.out.println("binnen de while van test");
			String message = null;
			//message = inFromOther.readLine();
			while((message = inFromOther.readLine()) != null) {
				System.out.println("binnen if in while in test");
					System.out.println("Bericht: " + message);
			}

			System.out.println("if geweest nieuwe te gaan");
			//boolean waiting = false;

			if(waiting == false) {
				String sendThis = null;
				Scanner in = new Scanner(System.in);
				System.out.println("Voer bericht in:");
				waiting = true;
				sendThis = in.nextLine();
				if(sendThis != null) {
					send(sendThis);
					waiting = false;
				}
			}
		//}
	}
	
	
	public void run() throws Exception {
		inFromOther = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
		outToOther = new DataOutputStream(clientSocket.getOutputStream());
		isTransmitting = true;
		while(isTransmitting == true ) {
			String sendThis;
			Scanner in = new Scanner(System.in);
			System.out.println("Voer bericht in:");
			sendThis = in.nextLine();
			send(sendThis);
			isTransmitting = false;
		}
		outToOther.close();
	}
	
	public void send(String sendThis) throws Exception {
		System.out.println("ga zo verzenden");
		String mes = "Seq=" + sequanceNumber + '\n'+ " ACK=" + ackNumber + '\n' + sendThis + "\n";
		System.out.println("TEST: "+ mes);
		outToOther.writeBytes(mes);
		
		sequanceNumber++;
	}
	
	public void receive() throws Exception {
		//inFromOther = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
		System.out.println("ga ontvangen");
		isTransmitting = true;
		String message = "";
		while((message = inFromOther.readLine())!= null) {
			
				System.out.println(message);
			
		}
	}


}
